<?php
/**
 * Created by PhpStorm.
 * User: yangxu
 * Date: 2016/5/4
 * Time: 9:56
 */

namespace Admin\Controller;
use Think\Controller;

class YatiAdsController extends Controller
{
    private $cat;
    public function __construct(){
        parent::__construct();
        if(!logAdmin(session('adminId'))) header('location:'.U('Admin/Index/login'));
        $this->cat=M('Category');
    }
   
    public function YatiAds_cata_list(){
        if(IS_GET){
            $type=I('get.level_one',1);
            $result=D('Adsmanage')->getCata($type);
            $CataOne=$this->getCataOne();
            $this->assign('CataOne',$CataOne);
            $this->assign('level_one_hid',$type);
            $this->assign('cata_list',$result);
            $this->display('AdsManage/Yati_Cat_List');
        }
    
    }
    
    public function getCataOne(){
        $result=M('category')->where('level=1')->select();
        if($result){
            return $result;
        }
    }
    public function editCat(){
             if($_GET){  
                 $Cat_id=I('get.id');
                 if($Cat_id){
                            $map['cat_id']=$Cat_id;
                            $result=M('adsmanage')->where($map)->find();
                            if($result){
                                $this->assign('diantiAds',$result['diantiAds']);
                                $this->assign('wenkuAds',$result['wenkuAds']);
                                $this->assign('id',$Cat_id);
                                $this->display('AdsManage/Yati_edit');
                            }
                            else{
                                $data['cat_id']=$Cat_id;
                                M('adsmanage')->data($data)->add();//广告表中没有记录时添加记录
                                $this->display('AdsManage/Yati_edit');
                            }
                 }
             }
    }
    
    public function save(){
        if(IS_POST){
           $data=I('post.');
           $map['cat_id']=$data['id'];
           $id=M('adsmanage')->where($map)->field('diantiAds,wenkuAds')->find();
           $string1=$id['diantiAds'];
           $string2=$id['wenkuAds'];
           if(!empty($string1)){
               $r=explode('/',$string1);
               $dir=dirname(dirname(dirname(dirname(__FILE__)))).'\Upload\pic_Ads\\'.$r[4];
               unlink($dir);//删除之前存储在upload文件中的图片。
           }
           if(!empty($string2)){
               $r=explode('/',$string2);
               $dir=dirname(dirname(dirname(dirname(__FILE__)))).'\Upload\pic_Ads\\'.$r[4];
               unlink($dir);//删除之前存储在upload文件中的图片。
           }
           $temp['diantiAds']=$data['diantiAds'];
           $temp['wenkuAds']=$data['wenkuAds'];
           $map['cat_id']=$data['id'];
           $result=M('adsmanage')->where($map)->data($temp)->save();
           if(0<=$result)
               $this->ajaxReturn('保存成功');
           else {
               $this->ajaxReturn('保存失败');
           }
        }
    }
  
    public function _list($parent_id=0){
        $catList=array();
        $list=$this->cat->where('parent_id='.$parent_id)->select();
        foreach($list as $key => $val){
            $val['son']=$this->_list($val['id']);
            $catList[]= $val;
        }
        return $catList;
    }
}